package cn.joker.springmyselfjoker.demos.web.service.impl;

import cn.joker.springmyselfjoker.demos.web.service.TruncateService;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author: jiawp
 * @date: 2024/9/2 16:01
 * @description:
 **/
@Service
public class TruncateServiceImpl implements TruncateService {
//    @Autowired
//    private TruncateDao truncateDao;

    @Override
    public String getTruncate(String q, String to){
        String res="";
        //发送http请求
        try {
            String appid ="20240407002016340";
            long salt =System.currentTimeMillis();
            //获取秘钥
            String sign = DigestUtils.md5Hex(appid +q + salt+"qRTmxT3urB1kcrhGoh78");
            //设置请求参数
            String urlStr ="https://fanyi-api.baidu.com/api/trans/vip/translate?q="
                    +q +"&from=auto&to="+to+"&appid="+appid
                    + "&salt=" + salt + "&sign=" + sign;
            URL url =new URL(urlStr);
            HttpURLConnection connection =  (HttpURLConnection)url.openConnection();
//            connection.setRequestMethod("POST");
//            connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            connection.setRequestMethod("GET");
            //设置 允许写入 请求体
//            connection.setDoOutput(true);
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code : " + responseCode);

            // 读取响应内容
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 打印响应结果
            System.out.println("Response Content : " + response.toString());
            res=response.toString();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return res;
    }



    // TODO 发送http请求 get请求
//        try {
//            String appid = "20240407002016340";
//            long salt = System.currentTimeMillis();
//            String query = URLEncoder.encode(truncate.getQ(), "UTF-8");
//            String to = URLEncoder.encode(truncate.getTo(), "UTF-8");
//
//            // 获取秘钥
//            String sign = DigestUtils.md5Hex(appid + truncate.getQ() + salt + "qRTmxT3urB1kcrhGoh78");
//
//            // 设置请求参数
//            String urlStr = "https://fanyi-api.baidu.com/api/trans/vip/translate?q="
//                    + query + "&from=auto&to=" + to + "&appid=" + appid
//                    + "&salt=" + salt + "&sign=" + sign;
//
//            URL url = new URL(urlStr);
//            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//            connection.setRequestMethod("GET");
//
//            int responseCode = connection.getResponseCode();
//            System.out.println("Response Code : " + responseCode);
//
//            // 读取响应内容
//            try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
//                String inputLine;
//                StringBuilder response = new StringBuilder();
//
//                while ((inputLine = in.readLine()) != null) {
//                    response.append(inputLine);
//                }
//
//                // 打印响应结果
//                System.out.println("Response Content : " + response.toString());
//            }
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
}
